package com.zyj.myapplication.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zyj.myapplication.Javabean.User;
import com.zyj.myapplication.Model.UserModel;
import com.zyj.myapplication.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private Button btn_reg, btn_login;
    private EditText et_username, et_password;
    private String error;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();//初始化
        initEvents();//初始化事件
    }

    public void initViews() {
        btn_login = (Button) findViewById(R.id.login);
        btn_reg = (Button) findViewById(R.id.reg);
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);


    }

    public void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);

            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String pwd = et_password.getText().toString();
                //取到用户输入的用户名和密码
                UserModel userModel = new UserModel();
                Call<User> call = userModel.login(username, pwd);
                //定义retrofit对象
                //创建访问API请求
                call.enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        String username = et_username.getText().toString();
                        String pwd = et_password.getText().toString();
                        error = response.body().getError();
                        if ("用户名或者密码不正确".equals(error)) {
                            Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                        } else {
                            if (username.equals("") || pwd.equals("")) {
                                Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                String userid = response.body().getId().trim();
                                Log.v("获取id", userid);
                                saveUser(userid);
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }


                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

                    }
                });


            }
        });

    }

    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username", et_username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", et_password.getText().toString());
        editor.commit();//提交修改
    }
}
